<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Request;
use think\Config;
use app\index\model\Course;
use app\index\model\Classes;
use app\index\model\cc;
use app\index\model\Signlist;
use app\index\model\Cossign;
use app\index\model\Ordsign;

class Sign extends Controller{
    public function index(){
        $hint = '';
        $this->assign('hint',$hint);
        if(Request::instance()->param()){
            $has = Request::instance()->param();
            // dump($has);
            $courseId = $has["courseId"];
        }else{
            $courseId = Session::get("courseId");
        }
        Session::set('courseId',$courseId);

        $Ustatus=Session::get('Ustatus');
        $this->assign('Ustatus',$Ustatus);

        $classlist = new Classes();
        $class = $classlist->coscla($courseId);
        // dump($class->toArray());
        $this->assign('class',$class);
        $this->assign('classlist',$class);
        $empty='<option value="">暂无班级</option>';
        $this->assign('empty',$empty);

        $signlist = new Course();
        if($Ustatus==1){
            $sign = $signlist->cossign($courseId);
            // dump($sign->toArray());
            $this->assign('signlist',$sign);

            $span = '<span>活动</span>
                    <span>管理</span>';
            $this->assign('span',$span);
        }
        if($Ustatus==0){
            $userId = Session::get("Uid");
            foreach($class->toArray() as $cla){
                // dump($cla);
                $data = $classlist->stucoscla($userId,$courseId,$cla['class_Id']);
                // dump($data->toArray());
                if(!empty($data->toArray())){
                    $sign = $signlist->stucossign($courseId,$data->toArray()[0]['class_Id']);
                    // dump($sign->toArray());
                    $this->assign('signlist',$sign);
                    break;
                }
            }
            $span = '<span>活动</span>';
            $this->assign('span',$span);
        }
        return $this->fetch();
    }

    public function addsign(){
        $hint = '';
        $this->assign('hint',$hint);

        $courseId = Session::get("courseId");
        $classlist = new Classes();
        $class = $classlist->coscla($courseId);
        // dump($class->toArray());
        $this->assign('classlist',$class);
        $empty='<option value="">暂无班级</option>';
        $this->assign('empty',$empty);

        return $this->fetch();
    }
    public function doaddsign(){
        if(Request::instance()->isPost()){
            $has = Request::instance()->post();
            $sname = $has["Name"];
            $classId = $has["class"];
            $begin = str_replace("T"," ",$has["Begin"]);
            $end = str_replace("T"," ",$has["End"]);
            $type = $has["Type"];
            $courseId = Session::get("courseId");

            $addsign = new Signlist();
            $add = $addsign->addsign($sname,$begin,$end,$type);
            $newsign = $addsign->newsign();

            $signId = $newsign->toArray()["0"]["Id"];
            $addcossign = new Cossign();
            $ad = $addcossign->addsign($courseId,$classId,$signId);
            if($add === false || $ad === false){
                $hint = '签到添加失败';
                Session::set('Hint',$hint);
                $this->redirect('index/Sign/addsign');
            }else{
                $this->redirect('index/Sign/index');
            }
        }
    }

    public function sign(){
        if(Request::instance()->param()){
            $has = Request::instance()->param();
            // dump($has);
            $signModel = new Signlist();
            $signmsg = $signModel->sign($has['sign_Id']);
            // dump($signmsg->toArray());
            $this->assign('signmsg',$signmsg[0]);
        }
        return $this->fetch();
    }

    public function issign(){
        Config::set("default_return_type","json");//设置返回的类型为json类型

        $requset = Request::instance();           //拿到前端的传入数据
        if($requset->isAjax()){
            $sign_Id = $requset->post("Id");
            $type = $requset->post("type");
            $user_Id = Session::get("Uid");
            if($type==4){
                $list = new Ordsign();
                $data = $list->issign($user_Id,$sign_Id);
                if($data->toArray()){
                    $retData = [
                        "flag"=>1,
                    ];
                }else{
                    $retData = [
                        "flag"=>0,
                    ];
                }
            }
            die(json_encode($retData));
        }
    }

    public function dosign(){
        Config::set("default_return_type","json");//设置返回的类型为json类型

        $requset = Request::instance();           //拿到前端的传入数据
        if($requset->isAjax()){
            $sign_Id = $requset->post("Id");
            $sign_Time = $requset->post("sign_Time");
            $type = $requset->post("type");
            $user_Id = Session::get("Uid");
            if($type==4){
                $ordsignModel = new Ordsign();
                $add = $ordsignModel->sign($user_Id,$sign_Id,$sign_Time);
                if($add === false){
                    $retData = [
                        "flag"=>0,
                    ];
                }else{
                    $retData = [
                        "flag"=>1,
                    ];
                }
            }
            die(json_encode($retData));
        }
    }

    public function signresult(){
        if(Request::instance()->param()){
            $has = Request::instance()->param();
            $hint='<tr><td class="text-c" colspan="3">暂无任何数据</td></tr>';
            $this->assign('hint',$hint);

            $cossignModel = new Cossign();
            $class = $cossignModel->student($has['sign_Id']);
            // dump($class->toArray());
            $this->assign('signresult',$class);
            // foreach($class as $a){
                // $a->result()->where('user_Id',$a['student_Id'])->select();
                // dump($a->toArray());
                // dump($a->result()->where('user_Id',$a['student_Id'])->find()->toArray());
            // }
        }
        return $this->fetch();
    }

    public function addclass(){
        $hint = '';
        $this->assign('hint',$hint);
        return $this->fetch();
    }

    public function doaddclass(){
        if(Request::instance()->isPost()){
            $has = Request::instance()->post();
            //var_dump($has);
            $courseId = Session::get("courseId");
            $Name = $has["Name"];

            $addclass = new Classes();
            $add = $addclass->addclass($Name);
            $newclass = $addclass->newclass();

            $class_Id = $newclass->toArray()["0"]["Id"];
            $addcc = new cc();
            $ad = $addcc->addclass($class_Id,$courseId);
        }
        if($add === false || $ad === false){
            $hint = '班级添加失败';
            Session::set('Hint',$hint);
            $this->redirect('index/Sign/addsign');
        }else{
            $this->redirect('index/Sign/index');
        }
    }
}
?>